package com.leetcode.根据算法进行分类.数学定理相关;

import java.util.*;

/**
 * @author: ZhouBert
 * @date: 2021/4/12
 * @description: 179. 最大数
 * https://leetcode-cn.com/problems/largest-number/
 * 当无法找到简单地规律时，只能通过朴素地证明了！
 * (o2.value + o1.value).compareTo((o1.value + o2.value));
 */
public class B_179_最大数 {
	static B_179_最大数 action = new B_179_最大数();

	public static void main(String[] args) {
//		test1();
		test2();
		test3();
	}


	public static void test1() {
		//
		int[] nums = new int[]{10, 2};
		String res = action.largestNumber(nums);

		System.out.println("res = " + res);
	}

	public static void test2() {
		//

		int[] nums = new int[]{8308, 8308, 830};
		String res = action.largestNumber(nums);

		System.out.println("res = " + res);

	}

	public static void test3() {
		//

		//System.out.println("res = " + res);
	}

	/**
	 * 错误的思路：
	 * eg.比较 3 与 30 时，以为规律就是 33 比较 30.错误地使用了规律。
	 *
	 * @param nums
	 * @return
	 */
	public String largestNumberWrong(int[] nums) {
		int len = nums.length;
		Set<Item> set = new TreeSet<Item>(new Comparator<Item>() {
			@Override
			public int compare(Item o1, Item o2) {
				return o1.value.compareTo(o2.value);
			}
		});
		for (int i = 0; i < len; i++) {
			StringBuilder sb = new StringBuilder();
			String value = nums[i] + "";
			sb.append(value);
			String firstNum = value.substring(0, 1);
			while (sb.length() < 9) {
				sb.append(firstNum);
			}
			set.add(new Item(sb.toString(), i, nums[i]));
		}
		Iterator<Item> iterator = ((TreeSet<Item>) set).descendingIterator();
		StringBuilder sbuilder = new StringBuilder();
		while (iterator.hasNext()) {
			sbuilder.append(iterator.next().realValue);
		}
		return sbuilder.toString();
		//int[] numsAfterExtracted = extractFirstNum(nums, 0, len);
	}

	/**
	 * (o2.value + o1.value).compareTo((o1.value + o2.value));
	 *
	 * @param nums
	 * @return
	 */
	public String largestNumber(int[] nums) {
		int len = nums.length;
		List<Item> list = new ArrayList<>(len);
		for (int i = 0; i < len; i++) {
			list.add(new Item(nums[i] + "", i, nums[i]));
		}
		Collections.sort(list, new Comparator<Item>() {
			@Override
			public int compare(Item o1, Item o2) {
				return (o2.value + o1.value).compareTo((o1.value + o2.value));
			}
		});
		StringBuilder sb = new StringBuilder();
		if (list.get(0).realValue == 0) {
			return "0";
		}
		for (int i = 0; i < len; i++) {
			sb.append(list.get(i).value);
		}
		return sb.toString();
	}

	static class Item {
		String value;
		Integer index;
		Integer realValue;

		public Item(String value, Integer index, Integer readValue) {
			this.value = value;
			this.index = index;
			this.realValue = readValue;
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;
			Item item = (Item) o;
			return Objects.equals(value, item.value) &&
					Objects.equals(index, item.index) &&
					Objects.equals(realValue, item.realValue);
		}

		@Override
		public int hashCode() {
			return Objects.hash(value, index, realValue);
		}
	}


}
